<?php
/**
 * CreateTime: 2023/2/1 14:17
 */

namespace App\Http\Controllers\MeOutboundSystem;

use App\Http\Requests\ListBasicRequest;
use App\Http\Requests\MeOutboundSystem\MeSendCallRequest;
use App\Models\MeOutboundSystem\MeOutboundSystemModel;
use App\Tools\Log\Log;
use App\Tools\USails\USailsBasic;

class MeOutboundSystemController
{

    /**
     * 回调接收地址
     */
    public function meCallBack(){
        $data = request()->all();
        Log::e(json_encode($data,256));
        $data['dialogues'] = json_encode($data['dialogues']);
        try {
            (new MeOutboundSystemModel())->add($data);
            return "1";
        }catch (\Exception $exception){
            Log::e($exception->getMessage());
        }
    }

    /**
     * 注册回调
     */
    public function registerCallBack(){
        (new USailsBasic())->registerCallBack();
    }


    /**
     * 获取坐席列表
     * @param ListBasicRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getExtensions(ListBasicRequest $request){
        $data = $request->all();
        return (new USailsBasic())->getExtensionsList($data['page'],$data['page_size']);
    }

    /**
     * 外呼
     */
    public function sendCall(MeSendCallRequest $request){
        $data = $request->all();
        return (new USailsBasic())->sendCall($data['callee'],$data['caller']);
    }

    public function getMeCdrsDetailsByUUid($uuid){
        return (new USailsBasic())->getMeCdrsDetailsByUUid($uuid);
    }

    public function getMeCdrsList(ListBasicRequest $request){
        $data = $request->all();
        $prams = [
            'page' => $data['page'],
            'pageSize' => $data['page_size']
        ];
        if(isset($data['filters'])){
            $filters = json_decode($data['filters'],true);
            if(isset($filters['callType'])){
                $prams['callType'] = $filters['callType'];
            }
            if(isset($filters['callee'])){
                $prams['callee'] = $filters['callee'];
            }
            if(isset($filters['caller'])){
                $prams['caller'] = $filters['caller'];
            }
            if(isset($filters['endDatetime'])){
                $prams['endDatetime'] = $filters['endDatetime'];
            }
            if(isset($filters['startDatetime'])){
                $prams['startDatetime'] = $filters['startDatetime'];
            }
        }
        return  (new USailsBasic())->getMeCdrsList($prams);
    }}